package com.yinda.dt.produce.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import java.io.File;

/**
 * Created by Administrator on 2017/3/30.
 */

public class ApplicationUtil {

    /**
     *
     * 检测是否需要升级
     * @param serverVersion    服务器给的版本号
     * @return
     */
    public static boolean isNeedUpdate(Context context,int serverVersion) {
        if(serverVersion>getThisAPPVersionCode(context)){
            return true;
        }
        return false;
    }

    /**
     * 获取当前APP版本号
     *
     * @param context
     * @return
     */
    public static int getThisAPPVersionCode(Context context) {
        PackageInfo info = getPackageInfo(context);
        int versionCode = info.versionCode;
        return versionCode;
    }

    public static String getThisAPPVersionName(Context context) {
        PackageInfo info = getPackageInfo(context);
        String versionName = info.versionName;
        return versionName;
    }

    /**
     * 获取App包 信息版本号
     *
     * @param context
     * @return
     */
    public static PackageInfo getPackageInfo(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo;
    }

    /**
     * 安装apk
     *
     * @param fileName
     */
    public static void installApk(Context context, String fileName) {
        File apkFile = new File(fileName);
        if (!apkFile.exists()) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://" + apkFile.getPath()), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}
